package itea.bignews.web;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.web.servlet.HttpServletBean;

import com.javaeedev.openid.Association;
import com.javaeedev.openid.Authentication;
import com.javaeedev.openid.OpenIdManager;

/**
 * 
 * @version $Id: GoogleLoginSvt.java 52 2009-09-11 09:32:44Z iteahere $
 * @author itea
 * @date 2009-8-14
 */
public class GoogleLoginSvt extends HttpServletBean {

	protected final Log log = LogFactory.getLog(this.getClass());
	
	private static final long serialVersionUID = 8466338775872830418L;

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		// for test
		setAuthTest(req);
		
		OpenIdManager manager = (OpenIdManager)req.getSession().getAttribute("openIdManager");;
		Association association = (Association)req.getSession().getAttribute("association");
		
		Authentication authentication = manager.getAuthentication(req, association.getRawMacKey());
		if(authentication != null)
			req.getSession().setAttribute("authentication", authentication);
		
		log.info(authentication);
	}

	private void setAuthTest(HttpServletRequest req) {
		Authentication a = new Authentication();
		a.setEmail("iteahere@gmail.com");
		a.setIdentity("http://big-new.com/itea");
		if(a != null)
			req.getSession().setAttribute("authentication", a);
	}
}
